 define(['zepto','common','layer','mylayer','postItem','echo','search','loginReg'], function ($,md_common,layer,mylayer,postItem,echo,search,md_loginReg) {
	var app={
		//公共事件
		init:function(){
			//判断是否存在
		    $.isInArray=function(data,keyword){
		        var result=-1;//不存在返回-1
		        keyword=keyword.toLowerCase();//小写
		        $.each(data,function(key,value){
		            if(value.keyword.toLowerCase()==keyword){
		                result=key;//存在返回索引
		                return false;
		            }
		        });
		        return result;
		    };
		    //ajaxpost请求
		    $.postAjax=function(url,data,success,error){
		    	$.ajax({url:url,data:data,dataType:'jsonp',jsonpCallback:'json_call',success:function(result){
					if(typeof success =="function"){success(result);}
					},error:function(){
							if(typeof error =="function"){error();}
					}
				});
		    };
		    //ajaxpost请求
		    $.postAjax=function(url,data,success,error){
		    	$.ajax({url:url,data:data,type:"post",dataType:"json",success:function(result){
					if(typeof success =="function"){success(result);}
					},error:function(){
						if(typeof error =="function"){error();}
					}
				});
		    };
			var _this=this;
			//侧边栏遮盖层
		    $('.mask_layer').click(function(){
		        $(this).hide();
		        $(".aside").removeClass('push');
		    });
		    //显示侧边栏
		    $("#menu").click(function(){
		        $('.mask_layer').show();
		        $(".aside").addClass('push');
		    });
		    $(".loadpage").on("click",function(){
		    	var scrollTop=$(window).scrollTop();
		        $(this).addClass("current").siblings("li").removeClass("current");
		        var page=$(this).attr("data-page");
		        var sitePage=$(".sitePage");
		        sitePage.addClass('current').siblings(".wrapPage").removeClass("current");
		        var c_page=sitePage.find(".page.current");
		        if(sitePage.find(".page"+page).size()==0){
		        	sitePage.append('<div class="page current page'+page+'"></div>');
		        	var s_page=sitePage.find(".page"+page);
		        }
		        var s_page=sitePage.find(".page"+page);
		        if($(c_page).attr("class")==$(s_page).attr("class")){
		            return false;
		        }
			    c_page.attr("scrollTop",scrollTop);
		        var that=this;
		        s_page.addClass("current").siblings(".page").removeClass("current");
		        if(!s_page.hasClass("is_load")){
		        	if(page=="Home"){
		        		_this.loadHome(function(){
		        			s_page.addClass("is_load");
		        		});
		        	}
		        	else if(page=="Search"){
		        		_this.loadSearch();
		        	}
		        	else if(page=="Book"){
		        		_this.loadBook(function(){
		        			s_page.addClass("is_load");
		        		});
		        	}
		        	else if(page=="User"){
		        		_this.loadUser();
		        	}
		        }
		        else{
		        	var top=s_page.attr("scrollTop");
		        	window.scrollTo(0,top);
		        }
		    });
		    $(".category").on("click",function(){
		    	$(".mask_layer").hide();
		        $(".aside").removeClass('push');
		    	var lay=md_common.showLoad(true,true);
		    	var id=$(this).attr("data-id");
		    	var title=$(this).attr("data-name");
		    	$.postAjax("/mobile/app/category/"+id,{},function(result) {
			        if(result.list){
			           layer.close(lay);
			           var html = template('product', result);
			            mylayer.showLayer({
			           		title:title,
			           		content:html,
			           		success:function(){
			           			productItem.init();
			           		},
			           		url:"/category/"+id
			           });
			        }
			    });
		    });
		    $(window).on("scroll",function(){
		    	if($(".pageHome").hasClass("current")){
		    		if(!$(".site_post").hasClass("ajax_load")){
		    			var total=$(".site_post").attr("total");
			    		var offset=$(".site_post").find(".post_item").size();
			    		if(offset<total){
			    			var top=$(".site_post").offset().top;
							var height=$(".site_post").height();
				    		if(top+height<=md_common.getScrollTop()+md_common.getWindowHeight()){
				    			$(".site_post").addClass("ajax_load");
				    			$(".pageHome").find(".list_loading").show();
				    			$.postAjax("/site",{'offset':offset},function(result) {
							        if(result.list){
							        	var html=_this.getPostInfo(result.list);
							            $(".site_post").append(html);
							            postItem.init();
							            $(".site_post").removeClass("ajax_load");
							            $(".pageHome").find(".list_loading").hide();
							        }
								});
				    		}
			    		}
		    		}
		    	}
		    	if($(".pageBook").hasClass("current")){
		    		if(!$(".book_list").hasClass("ajax_load")){
		    			var total=$(".book_list").attr("total");
			    		var offset=$(".book_list").find(".post_item").size();
			    		if(offset<total){
			    			var top=$(".book_list").offset().top;
							var height=$(".book_list").height();
				    		if(top+height<=md_common.getScrollTop()+md_common.getWindowHeight()){
				    			$(".book_list").addClass("ajax_load");
				    			$(".pageBook").find(".list_loading").show();
				    			$.postAjax("/book",{'offset':offset},function(result) {
							        if(result.list){
							        	var html=_this.getBookInfo(result.list);
							            $(".book_list").append(html);
							            postItem.init();
							            $(".book_list").removeClass("ajax_load");
							            $(".pageBook").find(".list_loading").hide();
							        }
								});
				    		}
			    		}
		    		}
		    	}
		    });
		},
		loadHome:function(success){
			var _this=this;
			$.postAjax("/site",{},function(result) {
		        if(result.list){
		        	var html=_this.getPostInfo(result.list);
		            $('.pageHome').html('<ul class="post_list site_post clearfix" total="'+result.total+'">'+html+'</ul>'+
		            '<div class="hide list_loading"></div>');
		            if(typeof success=="function"){
		            	success();
		            }
		            window.scrollTo(0,0);
		        }
			});
		},
		loadSearch:function(){
			$(".warp").hide();
			$(".search_bank").show();
			window.scrollTo(0,0);
		},
		loadBook:function(success){
			var _this=this;
			$.postAjax("/book",{},function(result) {
		        if(result.list){
		        	var html=_this.getBookInfo(result.list);
		            $('.pageBook').html('<ul class="post_list book_list clearfix" total="'+result.total+'">'+html+'</ul>'+
		            '<div class="hide list_loading"></div>');
		            if(typeof success=="function"){
		            	success();
		            }
		            window.scrollTo(0,0);
		        }
			});
		},
		loadUser:function(){
			var _this=this;
			if(window.localStorage){
				var u=sessionStorage.getItem("app_u");
				if(u){
					$(".pageUser").html('<div class="userBox clearfix">'+$(".account_bank").html()+'</div>');
					md_loginReg.initAccount();
				}
				else{
					$(".pageUser").html('<div class="userBox clearfix">'+$(".sign_box").html()+'</div>');
					md_loginReg.init();
				}
			}
			window.scrollTo(0,0);
		},
		getPostInfo:function(list){
			var html="";
			var length=list.length;
			for(var i=0;i<length;i++){
				html+='<li class="post_item"><a href="javascript:void(0)" class="showDetail" data-id="'+list[i]['id']+'">'+
						'<div class="box clearfix">'+
							'<div class="img">'+
								'<img src="'+list[i]['image']+'" />'+
							'</div>'+
							'<div class="info">'+list[i]['introduction']+'</div>'+
						'</div>'+
					'</a>'+
					'<div class="btn_box clearfix">'+
						'<a href="javascript:void(0)" class="item showDetail" data-id="'+list[i]['id']+'"><span class="btn_info view clearfix"><span class="icon icon-view"></span><span class="txt">浏览(<span class="text">'+list[i]['view']+'</span>)</span></span></a>'+
						'<a class="item  clearfix interact" href="javascript:void(0)" t="praiseup" data-id="'+list[i]['id']+'"><span class="btn_info parseup clearfix"><span class="icon icon-up"></span><span class="txt">点赞(<span class="text">'+list[i]['praiseup']+'</span>)</span></span><a>'+
						'<a href="javascript:void(0)" class="item showDetail" data-id="'+list[i]['id']+'"><span class="btn_info review clearfix"><span class="icon icon-pinglun"></span><span class="txt">评论(<span class="text">'+list[i]['review']+'</span>)</span></span></a>'+
					'</div>'+
				'</li>';
			}
			return html;
		},
		getBookInfo:function(list){
			var html="";
			var length=list.length;
			for(var i=0;i<length;i++){
				html+='<li class="post_item"><a target="_blank" href="'+list[i]['url']+'">'+
						'<div class="box clearfix">'+
							'<div class="img">'+
								'<img src="'+list[i]['image']+'" />'+
							'</div>'+
							'<div class="info">'+list[i]['title']+'</div>'+
						'</div>'+
					'</a>'+
					'<div class="btn_box clearfix">'+
						'<a href="javascript:void(0)" class="item showDetail" data-id="'+list[i]['id']+'"><span class="btn_info view clearfix"><span class="icon icon-view"></span><span class="txt">浏览(<span class="text">'+list[i]['view']+'</span>)</span></span></a>'+
						'<a class="item  clearfix interact" href="javascript:void(0)" t="praiseup" data-id="'+list[i]['id']+'"><span class="btn_info parseup clearfix"><span class="icon icon-up"></span><span class="txt">点赞(<span class="text">'+list[i]['praiseup']+'</span>)</span></span><a>'+
						'<a href="javascript:void(0)" class="item showDetail" data-id="'+list[i]['id']+'"><span class="btn_info review clearfix"><span class="icon icon-pinglun"></span><span class="txt">评论(<span class="text">'+list[i]['review']+'</span>)</span></span></a>'+
					'</div>'+
				'</li>';
			}
			return html;
		}
	};
	if(typeof app!=undefined){
		$(function(){
			app.init();
		});
	}
	return app;
});
